
package projetocompiladores;

import analisadorsintatico.GraphNode;
import analisadorsintatico.GraphNode.Tipo;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;

/**
 *
 * @author Anderson
 */
public class FigureGraph {

    private ArrayList<GraphFigureNode> graphFigures;

    public FigureGraph(GraphNode root) {
        graphFigures = new ArrayList<GraphFigureNode>();
        
        Point2D.Double currentPosition = new Point2D.Double(0, 0);

        GraphFigureNode figureNode = new GraphFigureNode("Start", Tipo.START, currentPosition);
        figureNode.setPosition(currentPosition);
        figureNode.addLink(new Point2D.Double(currentPosition.x, currentPosition.y+1));
        graphFigures.add(figureNode);

        currentPosition = new Point2D.Double(currentPosition.x, currentPosition.y+1);

        feedGraph(root, currentPosition);

        display();
    }

    public ArrayList<GraphFigureNode> getArray() {
        return graphFigures;
    }

    public void display() {
        for(GraphFigureNode node : graphFigures) {
            node.display();
        }
    }

    public double feedGraph(GraphNode root, Point2D.Double currentPosition) {

        for(int i = 0; i < root.getNodes().size(); i++) {
            GraphNode node = root.getNodes().get(i);
//            System.out.println("root tipo = " + root.getTipo());
            if(node.getTipo() == Tipo.NORMAL || node.getTipo() == Tipo.START) {
                GraphFigureNode figureNode = new GraphFigureNode(
                        node.getCommand(), node.getTipo(), currentPosition);
                figureNode.addLink(new Point2D.Double(currentPosition.x, currentPosition.y+1));
                figureNode.setPosition(currentPosition);
                graphFigures.add(figureNode);

                currentPosition = new Point2D.Double(currentPosition.x, currentPosition.y+1);

           } else if(node.getTipo() == Tipo.END) {
               GraphFigureNode figureNode = new GraphFigureNode(
                        node.getCommand(), Tipo.END, currentPosition);
                figureNode.setPosition(currentPosition);
                graphFigures.add(figureNode);

                currentPosition = new Point2D.Double(currentPosition.x, currentPosition.y+1);

           } else if(node.getTipo() == Tipo.IF) {
                System.out.println("node position = " + currentPosition);
                GraphFigureNode figureNode = new GraphFigureNode(
                        node.getCommand(), Tipo.IF, currentPosition);

                if(node.getNodes().size() == 1) {
                    if(node.getNodes().get(0).getTipo() == Tipo.TRUE) {
                        figureNode.addLink(new Point2D.Double(currentPosition.x-1, currentPosition.y+1));
//                        figureNode.addLink(new Point2D.Double(0, 0));
                    } else {
//                        figureNode.addLink(new Point2D.Double(0, 0));
                        figureNode.addLink(new Point2D.Double(currentPosition.x+1, currentPosition.y+1));
                    }
                } else if(node.getNodes().size() == 2) {
                        figureNode.addLink(new Point2D.Double(currentPosition.x-1, currentPosition.y+1));
                        figureNode.addLink(new Point2D.Double(currentPosition.x+1, currentPosition.y+1));
                }

                graphFigures.add(figureNode);

            } 
//            else if(node.getTipo() == Tipo.WHILE) {
//                GraphFigureNode figureNode = new GraphFigureNode(
//                        node.getCommand(), Tipo.WHILE, currentPosition);
//
//                figureNode.addLink(new Point2D.Double(currentPosition.x-1, currentPosition.y+1));
//                figureNode.addLink(new Point2D.Double(currentPosition.x+1, currentPosition.y+1));
//                graphFigures.put(currentPosition, figureNode);
//
//                graphFigures.put(new Point2D.Double(currentPosition.x+1, currentPosition.y+1), null);
//            }

            for(GraphNode child : node.getNodes()) {
                System.out.println("tipo = " + child.getTipo());
                if(child.getTipo() == Tipo.TRUE || root.getTipo() == Tipo.DO) {
                    double y = feedGraph(child, new Point2D.Double(currentPosition.x-1, currentPosition.y+1));
                    currentPosition = new Point2D.Double(currentPosition.x, y);
                } else if(child.getTipo() == Tipo.FALSE) {
                    double y = feedGraph(child, new Point2D.Double(currentPosition.x+1, currentPosition.y+1));
                    currentPosition = new Point2D.Double(currentPosition.x, y);
                }
            }

//            if(root.getTipo() == Tipo.WHILE) {
//                graphFigures.get(currentPosition)
//            }
        }

        return currentPosition.y;
    }
}
